/**
 * Copyright (c) 2018, wuyy All rights reserved.
 * wuyy 专有/保密源代码,未经许可禁止任何人通过任何渠道使用、修改源代码.
 * QQ:16349023
 * 项目名称 : com.wy2.eclipse.tool.ui
 * @version V1.0
 */
package com.wy2.eclipse.tool.ui.action;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.FileEditorInput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wy2.core.PropertiesDocument;
import com.wy2.eclipse.tool.lib.common.Constants;
import com.wy2.eclipse.tool.lib.common.IDEUtils;
import com.wy2.eclipse.tool.ui.UIActivator;
import com.wy2.eclipse.tool.ui.menuadapter.ProjectAdapter;
import com.wy2.generator.GenerateResult;
import com.wy2.generator.GeneratorByJdbcConnection;
import com.wy2.generator.config.GeneratorConsts;
import com.wy2.generator.resource.Messages;
import com.wy2.generator.test.DemoTest;

/**
 * @author wuyy<16349023@qq.com>
 * @date 2018年1月17日上午11:51:57
 * 
 */
public class CodeGenerateAction implements IObjectActionDelegate {
    
    private static final Logger logger = LoggerFactory.getLogger(CodeGenerateAction.class);

    private IWorkbenchPart targetPart;
    private IProject selectProject;
    private ISelection selection;

    /** 
     * @param action 
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
     */
    @Override
    public void run(IAction action) {
        //logger.info("name:{}, localtion: {} ,生成代码", selectProject.getName(),selectProject.getLocation().toOSString());
        
        IFile file = selectProject.getFile(Constants.GENERATE_CONFIG_NAME);        
        IEditorInput input=new FileEditorInput(file);
        try {
            IWorkbenchPage workPage = targetPart.getSite().getWorkbenchWindow().getActivePage();
            IEditorPart editorPart = workPage.openEditor(input, "com.wy2.eclipse.tool.ui.editors.CodeGenerateEditor");
            //editorPart.se
        } catch (PartInitException e1) {
            logger.info("打开代码生成编辑器异常", e1);
        }
        
        

    }

    /** 
     * @param action
     * @param selection 
     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
     */
    @Override
    public void selectionChanged(IAction action, ISelection selection) {
        this.selection=selection;
        
        StructuredSelection ss = (StructuredSelection) selection;
        ProjectAdapter adapter = (ProjectAdapter) ss.getFirstElement();
        if (adapter != null) {
            selectProject = adapter.getProject();
        }

    }

    /** 
     * @param action
     * @param targetPart 
     * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
     */
    @Override
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        this.targetPart=targetPart;

    }

}
